package com.jos.appium;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

public class AndroidWechat {

	public AndroidDriver<WebElement> getDriver() throws MalformedURLException {
		System.out.println("---getDriver start");
		DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
		desiredCapabilities.setCapability("platformName", "Android");
		desiredCapabilities.setCapability("deviceName", "127.0.0.1:7555");
		desiredCapabilities.setCapability("platformVersion", "6.0.1");
		desiredCapabilities.setCapability("appPackage", "com.tencent.mm");
		desiredCapabilities.setCapability("appActivity", ".ui.LauncherUI");
		desiredCapabilities.setCapability("sessionOverride", true);
		desiredCapabilities.setCapability("noReset", true);
		desiredCapabilities.setCapability("fullReset", false);
		desiredCapabilities.setCapability("unicodeKeyboard", true);
		desiredCapabilities.setCapability("newCommandTimeout", "30000");
		desiredCapabilities.setCapability("showChromedriverLog", true);
		desiredCapabilities.setCapability("recreateChromeDriverSessions", "True");

		URL remoteUrl = new URL("http://localhost:4723/wd/hub");
		AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(remoteUrl, desiredCapabilities);

		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		System.out.println("---getDriver end");
		return driver;
	}

	public void openMessage() {
		AndroidDriver<WebElement> driver = null;
		try {
			driver = getDriver();

			execClick(driver, "com.tencent.mm:id/tb", 3, "我");

			execClick(driver, "com.tencent.mm:id/ak2", 0, "支付");

			execSwipeDown(driver, -2800);
			execSwipeDown(driver, 2800);
			execSwipeDown(driver, 0);
			execSwipeDown(driver, 500);
			execSwipeDown(driver, 20);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		System.out.println("---openMessage end");
	}

	private void execClick(AndroidDriver<WebElement> driver, String id, int index, String comment)
			throws InterruptedException {
		List<WebElement> elementList;
		int elementLength;
		WebElement element;
		elementList = driver.findElements(By.id(id));
		elementLength = elementList == null ? 0 : elementList.size();
		System.out.println(id + " find=" + elementLength);
		element = elementList.get(index);
		element.click();
		System.out.println("--- click:" + comment);
		Thread.sleep(2 * 1000);
	}

	private boolean execSwipe(AndroidDriver<WebElement> driver, int startX, int startY, int endX, int endY)
			throws InterruptedException {
		Dimension size = driver.manage().window().getSize();
		int width = size.getWidth() - 10;
		int height = size.getHeight() - 10;

		if (startX < 0) {
			startX = 0;
		}
		if (startX > width) {
			startX = width;
		}

		if (startY < 0) {
			startY = 0;
		}
		if (startY > height) {
			startY = height;
		}

		int relX = startX - endX % width + startX;
		if (relX < 0) {
			endX = 0;
		} else {
			endX = relX % width;
		}

		int relY = startY - endY % height + startY;
		if (relY < 0) {
			endY = 0;
		} else {
			endY = relY % height;
		}

		System.out.println("---swipe: width=" + width + ", height=" + height + ", startX=" + startX + ", startY="
				+ startY + ", endX=" + endX + ", endY=" + endY);

		TouchAction action = new TouchAction(driver);
		action.press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)));
		action.moveTo(PointOption.point(endX, endY));
		action.release().perform();
		Thread.sleep(2 * 1000);

		return false;
	}

	private boolean execSwipeDown(AndroidDriver<WebElement> driver, int y) throws InterruptedException {
		Dimension size = driver.manage().window().getSize();
		int width = size.getWidth();
		int height = size.getHeight();
		return this.execSwipe(driver, width / 2, height, width / 2, y);
	}

	private boolean execSwipeUp(AndroidDriver<WebElement> driver, int y) throws InterruptedException {
		Dimension size = driver.manage().window().getSize();
		int width = size.getWidth();
		int height = size.getHeight();
		return this.execSwipe(driver, width / 2, 0, width / 2, -y);
	}

	private boolean execSwipeLeft(AndroidDriver<WebElement> driver, int x) throws InterruptedException {
		Dimension size = driver.manage().window().getSize();
		int width = size.getWidth();
		int height = size.getHeight();
		return this.execSwipe(driver, width, height / 2, x, height / 2);
	}

	private boolean execSwipeRight(AndroidDriver<WebElement> driver, int x) throws InterruptedException {
		Dimension size = driver.manage().window().getSize();
		int width = size.getWidth();
		int height = size.getHeight();
		return this.execSwipe(driver, 0, height / 2, -x, height / 2);
	}

	public static void main(String[] args) {
		AndroidWechat model = new AndroidWechat();

		model.openMessage();
	}

}
